<template>
	<view class="releaseIdle">
		<view class="content">
			<view class="line">
				<view class="line_left">
					求购标题
				</view>
				<view class="line_right">
					<input class="line_name" placeholder="输入标题" v-model="info.name" type="text">
					<image class="line_go" src="../static/index/go.png" mode=""></image>
				</view>
			</view>
			<view class="texta">
				<u--textarea v-model="info.content" height="150" placeholder="请输入具体的求购信息要求"></u--textarea>
			</view>
			<!-- <view class="line">
				<view class="line_left">
					所在城市
				</view>
				<view class="line_right">
					<input class="line_name" disabled="true" placeholder="请选择" v-model="citys" type="text">
				</view>
			</view> -->
			<view class="line" @click="showcate">
				<view class="line_left">
					分类
				</view>
				<view class="line_right">
					<input class="line_name" disabled="true" placeholder="选择分类" v-model="buy_cate_name" type="text">
					<image class="line_go" src="../static/index/go.png" mode=""></image>
				</view>
			</view>
			<view class="line">
				<view class="line_left">
					联系电话
				</view>
				<view class="line_right">
					<input class="line_name" placeholder="请输入" v-model="info.mobile" type="text">
				</view>
			</view>
			<view class="line">
				<view class="line_left">
					微信号
				</view>
				<view class="line_right">
					<input class="line_name" placeholder="请输入" v-model="info.wechat" type="text">
				</view>
			</view>
		</view>
		<!-- <tki-tree ref="cate" :range="ranges" confirmColor="#19B564" @confirm="confirme" /> -->
		<u-picker @confirm="confirm" @cancel="cateShow = false" :show="cateShow" :columns="buy_cate" keyName="name"></u-picker>
		
		<view class="agree">
			<view class="circle">
				<image v-if="select" @click="select = !select" class="select" src="https://www.stck.cc/my/selecta.png" mode="">
				</image>
				<image v-else @click="select = !select" class="select" src="https://www.stck.cc/my/noselect.png" mode="">
				</image>
			</view>
			<view class="other">同意</view>
			<view class="rule" @click="detail(16,'二手闲置物品求购公约')">《二手闲置物品求购公约》</view>
			
		</view>

		<view class="btn" @click="put">
			发 布
		</view>
	</view>
</template>

<script>
	import tkiTree from "@/components/tki-tree/tki-tree.vue"
	export default {
		components: {
			tkiTree
		},
		props: {
			pageParams: {
				type: Object,
				default: {}
			}
		},
		data() {
			return {
				ranges: [],
				select: false,
				value: '',
				info: {
					city: '',
					cate: '',
					mobile: '',
					price: '',
					content: ''
				},
				citys: '',
				// city: '郑州市',
				// district: '郑东新区',
				// lng: '113.665412',
				// lat: '34.757975',
				province: '',
				city: '黄冈市',
				district: '',
				lng: '114.872425',
				lat: '30.453722',
				fileList1: [],
				cate_id1: '',
				cate_id2: '',
				type: 0,
				id: '',
				
				cateShow:false,
				buy_cate:[],
				buy_cate_id:'',
				buy_cate_name:''
			}
		},
		mounted() {
			uni.setNavigationBarTitle({
				title: '求租求购'
			});
			
			this.getcate()
			if (this.pageParams && this.pageParams.data) {
				let data = JSON.parse(this.pageParams.data);
				this.fileList1 = data.images.map(value => {
					return {
						thumb: this.oss(value),
						url: value
					}
				})
				console.log(data,'datadata')
				this.info.name = data.name
				this.info.content = data.content 
				this.info.mobile = data.mobile
				this.info.wechat = data.wechat
				this.info.price = data.price
				
				this.buy_cate_id = data.buy_cate_id
				
				
				this.id = data.id
				this.type = 1
			}
		},
		methods: {
			detail(type, title) {
				uni.$u.route({
					url: 'pages/my/setting/article',
					params: {
						type: type,
						title
					}
				})
			},
			showcate() {
				// this.$refs.cate._show()
				this.cateShow = true
			},
			confirm(e) {
				console.log(e)
				this.buy_cate_name = e.value[0].name
				this.buy_cate_id = e.value[0].id
				this.cateShow = false
			},
			confirme(e) {
				if (e[0].parents[0]) {
					this.cate_id1 = e[0].parents[0].id 
					this.cate_id2 = e[0].id
					this.info.cate = e[0].parents[0].label + ' ' + e[0].label
				} else {
					this.cate_id1 = e[0].id
					this.info.cate = e[0].label
				}
			},
			getcate() {
				uni.$u.http.post('/buycate').then(res => {
					if (res.code == 1) {
						this.buy_cate = [res.data]
						
					}
				}).catch((res) => {

				})
			},
			put() {
				if (!this.select) return this.toast('请先同意公约')
				if (!this.info.name) return this.toast('请输入商品标题')
				if (!this.info.content) return this.toast('请输入商品描述')
				if (!this.city) return this.toast('请选择所在城市')
				// if (!this.info.cate) return this.toast('请选择分类')
				if (!this.info.mobile) return this.toast('请输入手机号')
				if (!uni.$u.test.mobile(this.info.mobile)) return this.toast('请输入正确的手机号')
				// if (!this.info.price) return this.toast('请输入商品价格')
				// if (this.fileList1.length == 0) return this.toast('请上传商品图片')

				let imagea = [];
				this.fileList1.forEach(item => {
					console.log(item)
					imagea.push(item.url)
				})

				let datas = ''

				if (this.type == 0) {
					datas = {
						type: 'add',
						// cate_id1: this.cate_id1,
						// cate_id2: this.cate_id2,
						name: this.info.name,
						images: imagea.join(','),
						price: this.info.price,
						content: this.info.content,
						province: this.province,
						city: this.city,
						district: this.district,
						lng: this.lng,
						lat: this.lat,
						id: this.id,
						mobile: this.info.mobile,
						wechat: this.info.wechat,
						buy_cate_id:this.buy_cate_id
					}
				} else {
					datas = {
						type: 'edit',
						id: this.id,
						// cate_id1: this.cate_id1,
						// cate_id2: this.cate_id2,
						name: this.info.name,
						images: imagea.join(','),
						price: this.info.price,
						content: this.info.content,
						province: this.province,
						city: this.city,
						district: this.district,
						lng: this.lng,
						lat: this.lat,
						mobile: this.info.mobile,
						wechat: this.info.wechat,
						buy_cate_id:this.buy_cate_id
					}
				}

				uni.$u.http.post('/postbuy', datas).then(res => {
					if (res.code == 1) {
						this.toast(res.msg)
						uni.$emit('edit')
						
						setTimeout(() => {
							this.back()
						}, 2000)
					}
				}).catch((res) => {

				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log(result)
					if (result) {
						let item = this[`fileList${event.name}`][fileListLen]
						this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: result.url
						}))
						fileListLen++
					} else {
						this[`fileList${event.name}`].splice(fileListLen, 1)
					}

				}
				console.log(this[`fileList${event.name}`])
			},
			uploadFilePromise(url) {
				console.log(url)
				let that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.Upload.uploadurl, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							type: 'image',
							token: uni.getStorageSync('token')
						},
						success: (res) => {
							let data = JSON.parse(res.data)
							if (res.statusCode === 200 && data.code === 1 && data.data) {
								resolve(data.data.url)
							} else {
								resolve()
							}
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.releaseIdle {
		/deep/ .u-textarea {
			background-color: #F9FAFB;
		}

		/deep/ .u-upload__wrap__preview__image {
			width: 130rpx !important;
			height: 130rpx !important;
		}



		.content {
			margin: 0 30rpx;

			.texta {
				margin: 30rpx 0;
			}

			.line {
				padding: 40rpx 0;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1rpx solid #D8D8D8;

				.line_left {
					font-size: 28rpx;
					font-weight: normal;
					color: #333333;
					line-height: 40rpx;
				}


				.line_right {
					display: flex;
					align-items: center;
					flex-direction: row;

					.line_name {
						margin-right: 10rpx;
						text-align: end;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #999999;
					}

					.line_hint {
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #999999;
					}

					.line_go {
						width: 24rpx;
						height: 24rpx;
					}
				}

			}


			.upimg {

				.up_tit {
					margin: 20rpx 0;
					font-size: 28rpx;
					font-weight: normal;
					color: #333333;
					line-height: 40rpx;
				}
			}
		}

		.treaty {
			margin-top: 50rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;

			.select {
				margin-right: 20rpx;
				width: 32rpx;
				height: 32rpx;
			}

			.treaty_name {
				font-size: 28rpx;
				font-weight: normal;
				color: #676767;
				line-height: 40rpx;
			}
		}

		.btn {
			margin: auto;
			margin-top: 30rpx;
			margin-bottom: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 686rpx;
			height: 96rpx;
			background: #FEBF4E;
			border-radius: 48rpx;
			font-size: 32rpx;
			font-weight: normal;
			color: #FEFFFE;
			line-height: 40rpx;
		}

	}
	.agree {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 30rpx 20rpx 20rpx;
		width: 100%;
		box-sizing: border-box;
	
		.circle {
			// width: 30rpx;
			// height: 30rpx;
			// box-sizing: border-box;
			// border-radius: 50%;
			// border: 1px solid #999999;
			margin-right: 10rpx;
			display: flex;
			align-items: center;
			justify-content: center;
	
	
			.select {
				width: 30rpx;
				height: 30rpx;
			}
		}
	
		.select {
			width: 22rpx;
			height: 22rpx;
		}
	
		.other {
			font-size: 22rpx;
			color: #999999;
		}
	
		.rule {
			font-size: 22rpx;
			color: #FF8007;
		}
	}
</style>
